home *** CD-ROM | disk | FTP | other *** search
- head 1.1;
- branch ;
- access ;
- symbols ;
- locks shirriff:1.1; strict;
- comment @ * @;
-
-
- 1.1
- date 90.10.27.13.45.50; author shirriff; state Exp;
- branches ;
- next ;
-
-
- desc
- @@
-
-
-
- 1.1
- log
- @Initial revision
- @
- text
- @/*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is given
- * to the University of California at Berkeley. The name of the University
- * may not be used to endorse or promote products derived from this
- * software without specific prior written permission. This software
- * is provided ``as is'' without express or implied warranty.
- */
-
- #ifndef lint
- static char sccsid[] = "@@(#)pclose.c 1.3 (Berkeley) 3/14/88";
- #endif /* not lint */
-
- #include <stdio.h>
- #include <signal.h>
- #include <sys/param.h>
- #include <sys/wait.h>
-
- #define tst(a,b) (*mode == 'r'? (b) : (a))
- #define RDR 0
- #define WTR 1
-
- extern char *malloc();
-
- static int *popen_pid;
- static int nfiles;
-
- FILE *
- mypopen(cmd,mode)
- char *cmd;
- char *mode;
- {
- int p[2];
- int myside, hisside, pid;
-
- if (nfiles <= 0)
- nfiles = getdtablesize();
- if (popen_pid == NULL) {
- popen_pid = (int *)malloc((unsigned) nfiles * sizeof *popen_pid);
- if (popen_pid == NULL)
- return (NULL);
- for (pid = 0; pid < nfiles; pid++)
- popen_pid[pid] = -1;
- }
- if (pipe(p) < 0)
- return (NULL);
- myside = tst(p[WTR], p[RDR]);
- hisside = tst(p[RDR], p[WTR]);
- if ((pid = vfork()) == 0) {
- /* myside and hisside reverse roles in child */
- (void) close(myside);
- if (hisside != tst(0, 1)) {
- (void) dup2(hisside, tst(0, 1));
- (void) close(hisside);
- }
- execl("/bin/sh", "sh", "-c", cmd, (char *)NULL);
- _exit(127);
- }
- if (pid == -1) {
- (void) close(myside);
- (void) close(hisside);
- return (NULL);
- }
- popen_pid[myside] = pid;
- (void) close(hisside);
- return (fdopen(myside, mode));
- }
-
- pabort()
- {
- extern int mflag;
-
- mflag = 0;
- }
-
- mypclose(ptr)
- FILE *ptr;
- {
- int child, pid, omask, pabort(), (*istat)();
- union wait status;
-
- child = popen_pid[fileno(ptr)];
- popen_pid[fileno(ptr)] = -1;
- (void) fclose(ptr);
- if (child == -1)
- return (-1);
- istat = signal(SIGINT, pabort);
- omask = sigblock(sigmask(SIGQUIT)|sigmask(SIGHUP));
- while ((pid = wait(&status)) != child && pid != -1)
- ;
- (void) sigsetmask(omask);
- (void) signal(SIGINT, istat);
- return (pid == -1 ? -1 : 0);
- }
- @
-